package com.itheima.a23;

import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.SimpleTypeConverter;
import org.springframework.core.GenericTypeResolver;
import org.springframework.format.Formatter;
import org.springframework.format.support.FormatterPropertyEditorAdapter;
import org.springframework.format.support.FormattingConversionService;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TestBeanWrapper {
    public static void main(String[] args) throws Exception {
        // 利用反射原理, 为 bean 的属性赋值
        MyBean target = new MyBean();
        BeanWrapperImpl wrapper = new BeanWrapperImpl(target);
        wrapper.setPropertyValue("a", "10");
        wrapper.setPropertyValue("b", "hello");
        wrapper.setPropertyValue("c", "1999/03/04");
        System.out.println(target);
        System.out.println("====================");
        Field[] declaredFields = MyBean.class.getDeclaredFields();
        MyBean myBean = new MyBean();
        String[] values= {"10","hello","1999/03/04"};
        for (int i = 0; i < declaredFields.length; i++) {
            Field declaredField = declaredFields[i];
            PropertyDescriptor descriptor = new PropertyDescriptor(declaredField.getName(), MyBean.class);
//            System.out.println(declaredField.getType().getName());
//            System.out.println(declaredField.getName());
            Object value = null;
            if (declaredField.getType() == int.class){
                value = Integer.valueOf(values[i]);
            }else if (declaredField.getType() == String.class){
                value = values[i];
            }else if (declaredField.getType() == Date.class){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                if (values[i] != null){
                    value = sdf.parse(values[i]);
                }
            }
            descriptor.getWriteMethod().invoke(myBean,value);
        }
        System.out.println(myBean);
        System.out.println("**************************************");

        MyBean myBean2 = new MyBean();
        String[] values2= {"20","hello2","2023/06/24"};
        Field[] declaredFields2 = MyBean.class.getDeclaredFields();
        SimpleTypeConverter typeConverter = new SimpleTypeConverter();
        for (int i = 0; i < declaredFields2.length; i++) {
            Field declaredField = declaredFields2[i];
            PropertyDescriptor descriptor = new PropertyDescriptor(declaredField.getName(), MyBean.class);
//            System.out.println(declaredField.getType().getName());
//            System.out.println(declaredField.getName());
            Object value = typeConverter.convertIfNecessary(values2[i],declaredField.getType());
            descriptor.getWriteMethod().invoke(myBean2,value);
        }
        System.out.println(myBean2);


    }

    static class MyBean {
        private int a;
        private String b;
        private Date c;

        public int getA() {
            return a;
        }

        public void setA(int a) {
            this.a = a;
        }

        public String getB() {
            return b;
        }

        public void setB(String b) {
            this.b = b;
        }

        public Date getC() {
            return c;
        }

        public void setC(Date c) {
            this.c = c;
        }

        @Override
        public String toString() {
            return "MyBean{" +
                   "a=" + a +
                   ", b='" + b + '\'' +
                   ", c=" + c +
                   '}';
        }
    }
}
